// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import Vuex from 'vuex'
import App from './App'
import router from './router'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.config.productionTip = false

Vue.use(ElementUI);
Vue.use(VueAxios, axios)
Vue.use(Vuex)


const store = new Vuex.Store({
  state: {
    account: "",
    propertys: [],
    current_property_index: "",
    current_property: null,
    agent_config :{
        jurisdiction: null,
        minPrice: "",
        maxPrice: "",
        choosen:null,
    },
    lottery_config:{
        sign_dateTime:'',
        live_broadcast_datetime:'',
        choose_datetime:''
    },
    under_config:{
      jurisdiction: null,
      choosen:null,
    },

    //假的数据库
   
    personal_info:{},
    db_park_pot:[],
    db_sales:[],
    db_user:[
      // {propertyindex当主键}
    ],
    db_activity:[],
    region_devide:[
    ],   //区域规划信息
    current_activity:null,
  },
  mutations: {
    insert_salse(state,item){
      if(state.db_sales.length==0){
        item.id = 0
      }else{
        item.id = state.db_sales[state.db_sales.length-1].id+1
      }
      state.db_sales.push(item)
    },
    set_under_config(state,config){
      state.under_config.jurisdiction = config.jurisdiction
      state.under_config.choosen = config.choosen 
    },
    set_lottery_config(state,config){
      state.lottery_config.sign_dateTime = config.sign_dateTime
      state.lottery_config.live_broadcast_datetime = config.live_broadcast_datetime
      state.lottery_config.choose_datetime = config.choose_datetime 
    },
    set_agent_config(state,config){
      state.agent_config.jurisdiction = config.jurisdiction
      state.agent_config.minPrice = config.minPrice
      state.agent_config.maxPrice = config.maxPrice
      state.agent_config.choosen = config.choosen
    },
    set_personal_info(state,user){
      state.personal_info = user
    },
    del_db_user(state,idcode){
      for(let i in state.db_user){
        if(state.db_user[i].idCode == idcode){
          state.db_user.splice(i,1)
          break
        }
      }
    },
    insert_to_db_user(state,user){
      state.db_user.push(user)
    },
    update_db_user(state,user){
      console.log(user)
      for(let i in state.db_user){
        if(state.db_user[i].idCode == user.idCode){
          state.db_user.splice(i,1,user)
          break
        }
      }
    },
    init_db_user(state,newdb){

    },
    add_park_info(state,item){
      state.db_park_pot.push(item)
    },
    init_park_pot(state,pot){
      state.db_park_pot.splice(0,state.db_park_pot.length)
      for(let i of pot){
        state.db_park_pot.push(i)
      }
      console.log(state.db_park_pot)
    },
    set_region_devide(state,info){
      state.region_devide = info
    },


    update_activity(state,data){
      for(let i in state.db_activity){
        if(state.db_activity[i].activityId==data.activityId){
          state.db_activity.splice(i,1,data)
          break
        }
      }
    },
    set_current_activity(state,item){
      state.current_activity = item
    },
    add_activity(state,item){
      state.db_activity.push(item)
    },
    del_current_activity(state,id){
      for(let i in state.db_activity){
        if(state.db_activity[i].activityId==id){
          state.db_activity.splice(i,1)
          break
        }
      }
    },




    setAccount(state, account) {
      state.account = account
    },
    setPropertys(state, propertys) {
      state.propertys = propertys
    },
    setCurrentPropertyIndex(state, index) {
      state.current_property_index = index
    },
    setCurrentProperty(state, property) {
      state.current_property = property
    }
  },
  geeters: {
    getCurrentPropery(state) {


    }
  },
  actions: {

    getRegionInfo({ state }) {
      let id = 10000
      return new Promise((resolve,reject)=>{
        let result = [{children:[]}]
        let region_is_valid = (region)=>{
          return region != "" && region!= "null"
        }
        let url = "http://47.99.124.245:8082/getRegion?propertyname=" + state.current_property.area;
        axios({
          method: "get",
          url: url,
        })
          .then((res) => {
         
            let data = res.data.data;
            let layer_first = new Map();
            if (data != null) {
              for (let i of data) {
                if (layer_first.has(i.region1)) {
                  let layer_sec = layer_first.get(i.region1);
  
                  if (region_is_valid(i.region2)) {
                    if (!layer_sec.has(i.region2)) {
                      layer_sec.set(i.region2, []);
                    } 
  
                    if (region_is_valid(i.region3)) {
                      let layer_third = layer_sec.get(i.region2);
                      layer_third.push(i.region3);
                    }
                  }
                } else {
                  layer_first.set(i.region1, new Map());
                  if (region_is_valid(i.region2)) {
                    let layer_sec = layer_first.get(i.region1);
                    layer_sec.set(i.region2, []);
                    if (region_is_valid(i.region3)) {
                      let layer_third = layer_sec.get(i.region2);
                      layer_third.push(i.region3);
                    }
                  }
                }
              }
              for (let i of layer_first) {
                let obj = {
                  id: id++,
                  label: i[0],
                  value: i[0],
                  showInput: false,
                  children: [],
                };
                //已经见底了
                if (i[1].size == 0) {
                  if (obj.children.length == 0) {
                    delete(obj, "children");
                  }
                  result[0].children.push(obj);
                } else {
                  for (let j of i[1]) {
                    let subobj = {
                      id: id++,
                      label: j[0],
                      value: j[0],
                      showInput: false,
                      children: j[1],
                    };
                    if (subobj.children.length == 0) {
                      delete(subobj, "children");
                    }
                    obj.children.push(subobj);
                  }
  
                  if (obj.children.length == 0) {
                    delete(obj, "children");
                  }
                  result[0].children.push(obj);
                }
              }
              resolve(result)
            }else{
              resolve({})
            }
          }).catch((err) => {
            reject(err)
          });
      })
 
    }
  }
})
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
